package com.oyjd.beauty.business.goods.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.beauty.bean.po.Goods;
import com.oyjd.beauty.bean.po.GoodsBody;
import com.oyjd.beauty.bean.po.GoodsKid;
import com.oyjd.beauty.bean.po.TypeSort;
import com.oyjd.beauty.bean.vo.GoodsTypeVO;
import com.oyjd.beauty.bean.vo.GoodsVO;
import com.oyjd.beauty.business.goods.dao.GoodsDao;
import com.oyjd.beauty.business.goods.service.GoodsService;
import com.oyjd.beauty.business.goods.vo.GoodsVOEx;
import com.oyjd.beauty.fw.action.Page;
import com.oyjd.beauty.fw.util.FileOP;

/**
 * 
 * 商品信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Transactional(readOnly = true)
public class GoodsServiceImpl implements GoodsService {
	private GoodsDao dao;

	@Autowired
	public void setDao(GoodsDao dao) {
		this.dao = dao;
	}

	private String picPath;

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	/**
	 * 获取商品信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<GoodsVOEx> getList(Page<GoodsVOEx> page) {
		return dao.getList(page);
	}

	/**
	 * 根据ID获取商品信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public GoodsVO getGoodsVO(String goodsId) {
		return dao.getGoodsVO(goodsId);
	}

	/**
	 * 根据商品ID获取性格ID列表
	 * 
	 * @param goodsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getKid(String goodsId) {
		String hql = "select kidId from " + GoodsKid.class.getName() + " where goodsId=?";
		return (List<String>) dao.queryByHql(hql, new Object[] { goodsId });
	}

	/**
	 * 根据商品ID获取体型ID列表
	 * 
	 * @param goodsId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getBody(String goodsId) {
		String hql = "select bodyId from " + GoodsBody.class.getName() + " where goodsId=?";
		return (List<String>) dao.queryByHql(hql, new Object[] { goodsId });
	}

	/**
	 * 增加商品信息
	 * 
	 * @param goodsTypeId
	 *            商品类ID
	 * @param link
	 *            链接
	 * @param title
	 *            标题
	 * @param suggest
	 *            建议
	 * @param picName
	 *            图片名称
	 * @param picFile
	 *            图片文件
	 * @param kids
	 *            性格列表
	 * @param bodys
	 *            体型列表
	 * @param user
	 *            当前用户
	 * @throws Exception
	 */
	@Transactional(readOnly = false)
	public void add(String goodsTypeId, String link, String title, String suggest, String picName, File picFile, String[] kids, String[] bodys, String user) throws Exception {
		String pic = FileOP.getNewHashName(picName, "pic_");

		// 保存商品
		Date date = new Date();
		Goods goods = new Goods();
		goods.setGoodsTypeId(goodsTypeId);
		goods.setLink(link);
		goods.setTitle(title);
		goods.setSuggest(suggest);
		goods.setPic(pic);
		goods.setCreator(user);
		goods.setCreateTime(date);
		goods.setLastEditor(user);
		goods.setLastUpdateTime(date);
		goods.setStatus(0);
		dao.create(goods);

		//
		for (String kidId : kids) {
			GoodsKid kid = new GoodsKid();
			kid.setGoodsId(goods.getGoodsId());
			kid.setKidId(kidId);
			dao.create(kid);
		}
		//
		for (String bodyId : bodys) {
			GoodsBody gb = new GoodsBody();
			gb.setGoodsId(goods.getGoodsId());
			gb.setBodyId(bodyId);
			dao.create(gb);
		}

		// 文件保存
		String path = picPath + File.separator + pic;
		FileOP.writeFile(path, picFile);
	}

	/**
	 * 修改商品信息
	 * 
	 * @param goodsId
	 *            商品ID
	 * @param goodsTypeId
	 *            商品类ID
	 * @param link
	 *            链接
	 * @param title
	 *            标题
	 * @param suggest
	 *            建议
	 * @param picName
	 *            图片名称
	 * @param picFile
	 *            图片文件
	 * @param kids
	 *            性格列表
	 * @param bodys
	 *            体型列表
	 * @param user
	 *            当前用户
	 * @throws Exception
	 */
	@Transactional(readOnly = false)
	public void edit(String goodsId, String goodsTypeId, String link, String title, String suggest, String picName, File picFile, String[] kids, String[] bodys, String user) throws Exception {
		String pic = FileOP.getNewHashName(picName, "pic_");

		// 保存商品
		Date date = new Date();
		Goods goods = (Goods) dao.queryById(Goods.class, goodsId);
		goods.setGoodsId(goodsId);
		goods.setGoodsTypeId(goodsTypeId);
		goods.setLink(link);
		goods.setTitle(title);
		goods.setSuggest(suggest);
		if (!"".equals(pic)) {
			goods.setPic(pic);
		}
		goods.setLastEditor(user);
		goods.setLastUpdateTime(date);
		dao.update(goods);

		//
		dao.executeHql("delete from " + GoodsKid.class.getName() + " where goodsId=?", new Object[] { goods.getGoodsId() });
		for (String kidId : kids) {
			GoodsKid kid = new GoodsKid();
			kid.setGoodsId(goods.getGoodsId());
			kid.setKidId(kidId);
			dao.create(kid);
		}
		//
		dao.executeHql("delete from " + GoodsBody.class.getName() + " where goodsId=?", new Object[] { goods.getGoodsId() });
		for (String bodyId : bodys) {
			GoodsBody gb = new GoodsBody();
			gb.setGoodsId(goods.getGoodsId());
			gb.setBodyId(bodyId);
			dao.create(gb);
		}

		// 文件保存
		if (!"".equals(pic)) {
			String path = picPath + File.separator + pic;
			FileOP.writeFile(path, picFile);
		}
	}

	/**
	 * 删除商品信息
	 * 
	 * @param ids
	 *            多个ID
	 * @param user
	 *            当前用户
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String[] ids, String user) {
		dao.remove(ids, user);
	}

	/**
	 * 获取图片文件流
	 * 
	 * @param picName
	 * @return
	 */
	public byte[] getSou(String picName) throws Exception {
		return FileOP.readFile(picPath + File.separator + picName);
	}

	/**
	 * 获取大分类
	 * 
	 * @return
	 */
	public List<GoodsTypeVO> getTopType() {
		return dao.getTopType();
	}

	/**
	 * 交换资料的排序位置
	 * 
	 * @param id1
	 *            Id1商品ID
	 * @param id2
	 *            Id2商品ID
	 * @param type
	 *            类型
	 */
	@Transactional(readOnly = false)
	public void exchangePos(String id1, String id2, String type) {
		String hql = "from " + TypeSort.class.getName() + " where goodsTypeId=? and goodsId=?";
		TypeSort o1 = (TypeSort) dao.queryByHql(hql, new Object[] { type, id1 }).get(0);
		TypeSort o2 = (TypeSort) dao.queryByHql(hql, new Object[] { type, id2 }).get(0);
		int n = o1.getOrderNo();
		o1.setOrderNo(o2.getOrderNo());
		o2.setOrderNo(n);
		dao.update(o1);
		dao.update(o2);
	}

	/**
	 * 将资料的排至首位
	 * 
	 * @param id
	 *            Id商品ID
	 * @param type
	 *            类型
	 */
	@Transactional(readOnly = false)
	public void moveFirst(String id, String type) {
		//
		int max = dao.getMaxOrderNo(type);
		//
		String hql = "from " + TypeSort.class.getName() + " where goodsTypeId=? and goodsId=?";
		TypeSort ts = (TypeSort) dao.queryByHql(hql, new Object[] { type, id }).get(0);
		ts.setOrderNo(max + 1);
		dao.update(ts);
	}

	/**
	 * 新增类型排序元素
	 * 
	 * @param ids商品ID
	 * @param souType
	 * @param type
	 */
	@Transactional(readOnly = false)
	public void addOrder(String[] ids, String type) {
		//
		int max = dao.getMaxOrderNo(type);
		//
		int n = max + ids.length;
		for (int i = 0; i < ids.length; i++) {
			TypeSort ts = new TypeSort();
			ts.setGoodsTypeId(type);
			ts.setGoodsId(ids[i]);
			ts.setOrderNo(n - i);
			dao.create(ts);
		}
	}

	/**
	 * 删除类型排序元素
	 * 
	 * @param ids商品ID
	 * @param souType
	 * @param type
	 */
	@Transactional(readOnly = false)
	public void removeOrder(String[] ids, String type) {
		String hql = "delete from " + TypeSort.class.getName() + " where goodsTypeId=? and (";
		List<String> para = new ArrayList<String>();
		para.add(type);
		for (int i = 0; i < ids.length; i++) {
			hql += i == 0 ? "" : " or ";
			hql += "goodsId=?";
			para.add(ids[i]);
		}
		hql += ")";
		dao.executeHql(hql, para.toArray(new Object[0]));
	}

	/**
	 * 获取分类排序的列表
	 * 
	 * @param page
	 * @param type
	 * @return
	 */
	public Page<GoodsVOEx> getTypeList(Page<GoodsVOEx> page, String type) {
		return dao.getTypeList(page, type);
	}

	/**
	 * 获取差异的列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<GoodsVOEx> getLeftList(Page<GoodsVOEx> page, String type) {
		return dao.getLeftList(page, type);
	}
}
